package com.hugoo.core.cip;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 *
 * @author Lovelock.Chan
 */
public class LxCipher {

    private static String DES = "DES";

    public static String encrypt(String data) throws Exception {
        String scod = Long.toString(System.currentTimeMillis(), 36);
        byte[] bt = encrypt(data.getBytes(), spmkey(scod).getBytes());
        String strs = Base64.encodeBase64URLSafeString(bt);
        return strs + "!" + scod;
    }

    public static String MD5(String s) {
        try {
            byte[] btInput = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    private static String spmkey(String key) {
        char[] x = key.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i : e) {
            sb.append(x[i]);
        }
        return sb.toString();
    }

    public static String decrypt(String data) throws IOException,
            Exception {
        if (data == null) {
            return null;
        }
        String x[] = data.split("!");
        String scod = x[1];
        byte[] buf = Base64.decodeBase64(x[0]);
        byte[] bt = decrypt(buf, spmkey(scod).getBytes());
        return new String(bt);
    }

    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }

    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }
    private final static int[] e = {7, 2, 4, 0, 5, 3, 1, 6};
    private final static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
}
